<template>
  <div class="home">
    <van-nav-bar
      title="搜索"
      class="page-nav-bar"
      fixed
    >
      <template #title>
        <van-button @click="$router.push('./search')" class="search-btn" icon="search" type="primary">主要按钮</van-button>
      </template>
    </van-nav-bar>

    <!-- 滑动导航 -->
    <van-tabs sticky offset-top="1.22rem" v-model="active" swipeable animated class="home-tab">
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <article-list :channelId="item.id"></article-list>
      </van-tab>
      <template #nav-right>
        <div class="placeholder-box"></div>
        <div class="right-menu" @click="isShowEditPopup=true">
          <i class="toutiao toutiao-gengduo"></i>
        </div>
      </template>
    </van-tabs>

    <!-- 编辑频道弹层 -->
    <van-popup
      closeable
      close-icon-position="top-left"
      v-model="isShowEditPopup"
      position="bottom"
      :style="{ height: '90%' }"
      class="editPopup"
    >
      <channel-edit :recommendChannels="recommendChannels" @onGo = "onGo" @setActive="active--" :channels="channels" :active="active"></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import ChannelEdit from './components/ChannelEdit.vue'
import { getAllChannels } from '@/api/article.js'
import { getUserSelfChannels } from '@/api/user.js'
import ArticleList from './components/ArticleList.vue'
import { getStorage } from '@/utils/storage.js'
import { mapState } from 'vuex'
import { Toast } from 'vant'
export default {
  components: { ArticleList, ChannelEdit },
  name: 'Home',
  data () {
    return {
      active: 0,
      channels: [],
      isShowEditPopup: false,
      allChannels: []
    }
  },
  created () {
    this.loadUserSelfChannels()
    this.loadAllChannels()
  },
  computed: {
    ...mapState(['user']),
    recommendChannels () {
      // 对allChannels进行筛选
      // allChannels的每一项 是否在channels里,如果没有在channels,属于recommendChannels
      // const arr = this.allChannels.filter(item => {
      //   const flag = this.channels.some(item2 => {
      //     return item2.id === item.id
      //   })
      //   return !flag
      // })
      // return arr
      return this.allChannels.filter(item => {
        return !this.channels.some(item2 => item2.id === item.id)
      })
    }

  },
  methods: {
    // 加载所有频道列表
    async loadAllChannels () {
      try {
        // 如果未登录且本地有数据
        const storageData = getStorage('MY-CHANNELS')
        if (!this.user && storageData) {
          this.allChannels = storageData
          return false
        }
        const res = await getAllChannels()
        this.allChannels = res.channels
      } catch (error) {
        Toast('加载所有频道失败!')
      }
    },
    onGo (index) {
      this.active = index
      this.isShowEditPopup = false
    },
    async loadUserSelfChannels () {
      try {
        const res = await getUserSelfChannels()
        this.channels = res.channels
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang="less">
.home{
  padding-top: 80px;
  .search-btn{
    width: 555px;
    height: 64px;
    background: rgba(255, 255, 255, .2);
    border: none;
    border-radius: 32px;
  }
  /deep/.van-nav-bar__title{
    max-width: unset;
  }
  .van-icon-search{
    color: #fff;
  }

  /deep/.home-tab{
    .van-tab{
      min-width: 200px;
      border-right:1px solid #EDEFF3;
      height: 82px;
      .van-bar__text{
        font-size: 28px;
        color: #777;
      }
    }

    .van-tabs__wrap{
      height: 82px;
      border-bottom: 1px solid #EDEFF3;
    }

    .van-tab--active{
      .van-bar__text{
        font-size: 30px;
        color: #333;
      }
    }

    .van-tabs__line{
      width: 31px;
      height: 6px;
      background: #3296FA;
      border-radius: 3px;
      bottom: 8px;
    }

    .van-tabs__nav--line {
      padding-bottom: 0;
    }

    .right-menu{
      width: 66px;
      height: 82px;
      position: fixed;
      right: 0;
      background: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
      i{
        font-size: 40px;
      }
      &::before {
        content: '';
        width: 1px;
        height: 58px;
        position: absolute;
        left: 0;
        top: 12px;
        background-image: url(~@/assets/images/gradient-gray-line.png);
        background-size: 100% 100%;
      }
    }
    // 占位
    .placeholder-box{
      width: 66px;
      height: 82px;
      flex-shrink: 0;
    }
  }

  // 底部弹出层样式
  .editPopup{
    box-sizing: border-box;
    padding-top: 100px;
  }
}

</style>
